package com.buer.storage.business.service.impl;


import com.buer.storage.business.dao.DorderDao;
import com.buer.storage.business.domain.Dorder;
import com.buer.storage.business.domain.Dorderdetails;
import com.buer.storage.business.domain.Stock;
import com.buer.storage.business.dto.DorderDto;
import com.buer.storage.business.dto.DorderdetailsDto;
import com.buer.storage.business.dto.OutStorageAllDto;
import com.buer.storage.business.dto.OutStorageDto;
import com.buer.storage.business.service.DorderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class DorderServiceImpl implements DorderService {
    @Resource
    private DorderDao dorderDao;

    @Override
    public List<Stock> findStock() {
        return dorderDao.findStock();
    }

    @Override
    public List<Dorder> selectByAll(String dorderNum, Integer dtypeId, String prodName, String beginTime, String endTime) {

        return dorderDao.selectByAll(dorderNum,dtypeId,prodName,beginTime,endTime);
    }

    @Transactional
    @Override
    public int insert(OutStorageDto outStorageDto) {

        //outStorageDto.setOutStatus(0);
        int result = dorderDao.insertOrder(outStorageDto);
        for (int i = 0; i < outStorageDto.getOutProds().length; i++) {
            Dorderdetails outProd = outStorageDto.getOutProds()[i];
            outProd.setDorderOutid(outStorageDto.getOutId());
        }

        dorderDao.insertDetails(outStorageDto.getOutProds());

        return result;
    }

    @Override
    public List<DorderDto> findOrderAll() {
        List<DorderDto> outAll = dorderDao.findOrderAll();
        return outAll;
    }

    @Transactional
    @Override
    public int deleteOutStorageById(int outId) {
        int deletedorderById = dorderDao.deleteOutStorageById(outId);
        return deletedorderById;
    }

    @Override
    public int deleteDorderdetails(int deliveryId) {
        int deletedorderdetails = dorderDao.deleteDorderdetails(deliveryId);
        return deletedorderdetails;
    }

    @Override
    @Transactional
    public int updateDorderdetails(OutStorageDto outStorageDto) {
        Integer outId = outStorageDto.getOutId();
        dorderDao.deleteDorderByOutId(outId);
        dorderDao.deleteDorderDetailsByOutId(outId);
        // 第二部再插入
        outStorageDto.setOutStatus(0);
        int result = dorderDao.insertOrder(outStorageDto);
        for (int i = 0; i < outStorageDto.getOutProds().length; i++) {
            Dorderdetails outProd = outStorageDto.getOutProds()[i];
            outProd.setDorderOutid(outStorageDto.getOutId());
        }
        dorderDao.insertDetails(outStorageDto.getOutProds());
        return result;
    }


    //根据订单表id查询入库信息
    @Override
    public List<OutStorageAllDto> findOutAll(int outId) {
        List<OutStorageAllDto> all = dorderDao.findOutAll(outId);
        if (all.size() > 0) {
            for (int i = 0; i < all.size(); i++) {
                DorderdetailsDto[] info = dorderDao.findOrderDetailInfoByOutId(all.get(i).getOutId());
                if (info.length > 0) {
                    all.get(i).setOutProds(info);
                }
            }
        }
        return all;
    }

    //出库单表减去
    @Override
    @Transactional
    public int submitAddBuerStock(int outId) {
        DorderdetailsDto[] outProds = dorderDao.findDordertailsAll(outId);

        int result = 0;
        for (int i = 0; i < outProds.length; i++) {
            result = 1;
            DorderdetailsDto outProd = outProds[i];

            //先查找prodArea,prodDepart,prodId
            Stock stock = dorderDao.queryBuerStockByParam(outProd);

            //如果stock表里没有的话就插入
            // dorderDao.insertStock(outProd);

            //更新累加
            Integer prodNum = outProd.getProdNum();

            Integer newProdNum = stock.getProdStocknum() - prodNum;

            Integer stockId = stock.getStockId();

            //System.out.println(stockId);
            stock.setProdStocknum(newProdNum);

            if (newProdNum == 0) {
                dorderDao.deleteStock(stockId);
            } else {
                dorderDao.updateAddSumStock(stock);
                dorderDao.updateStatus(outId);
            }
        }

        return result;
    }
}
